<template>
    <el-divider/>
    <span>患者信息：</span>
    <el-tag class="ml-2">姓名：{{ patientInfo.realName }}</el-tag>
    <el-tag class="ml-2">病历号：{{ patientInfo.caseNumber }}</el-tag>
    <el-tag class="ml-2">年龄：{{ patientInfo.age }}</el-tag>
    <el-tag class="ml-2">性别：{{ patientInfo.gender }}</el-tag>
    <el-divider/>
    <el-icon>
        <DocumentChecked/>
    </el-icon>
    <span>检查项目</span>
    <el-divider/>
    <el-table
            ref="singleTableRef"
            :data="techList"
            style="width: 100%"
            highlight-current-row
            @current-change="handleCurrentChange"
    >
        <el-table-column property="techCode" label="检查编码" width="360"/>
        <el-table-column property="techName" label="检查名称" width="360"/>
        <el-table-column property="techFormat" label="检查规格"/>
        <el-table-column property="techPrice" label="单价"/>
        <el-table-column property="techType" label="费用分类"/>

    </el-table>
    <div style="margin-top: 20px">
        <el-tag class="ml-2" size="large">已选择的检查项目：{{ selectTemp.techName }}</el-tag>
    </div>
    <el-divider/>
    <div>确认检查科室和检查医生</div>
    <table class="regist-info">
        <tr>
            <td>挂号科室</td>
            <td>
                <el-input @click="departmentTable = true" v-model="deptmentName" placeholder="请选择科室"/>
            </td>
            <td>医生选择</td>
            <td>
                <el-select class="m-2" placeholder="请选择"
                           @change="queryDoc" v-model="empId">
                    <el-option
                            v-for="item in empList"
                            :key="item.id"
                            :label="item.realname"
                            :value="item.id"
                    />
                </el-select>
            </td>
        </tr>
    </table>
    <el-drawer
            v-model="departmentTable"
            title="请选择科室"
            direction="rtl"
            size="50%"
            @open="queryAllDepartment">
        <el-table :data="departmentList" style="width: 100%">
            <el-table-column label="科室ID" prop="deptCode"/>
            <el-table-column label="科室名称" prop="deptName"/>
            <el-table-column label="科室种类" prop="deptType"/>
            <el-table-column align="right">
                <template #header>
                    <el-input v-model="search" size="small" placeholder="Type to search"/>
                </template>
                <template #default="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row);queryDoc()">
                        选择
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
    </el-drawer>
    <el-divider/>
    <el-button type="primary" @click="beginCheck">开始检查</el-button>
    <el-button type="primary" @click="empId=''">重置医生</el-button>
</template>

<script setup>
import {DocumentChecked} from "@element-plus/icons-vue";
import {provide, ref} from "vue";
import axios from "axios";
import {useRoute} from "vue-router";

axios.defaults.baseURL = "http://localhost:8092/"
provide("axios", axios)
//医生列表
const empList = ref([])
//排班信息
const schedulingInfo = ref({})
//科室列表
const departmentList = ref([])
const departmentTable = ref(false)
//科室名称
const deptmentName = ref("")
//患者信息
const patientInfo = ref({
    caseNumber: '',
    realName: '',
    age: '',
    gender: ''
})
patientInfo.value.caseNumber = useRoute().query.caseNumber
//医技项目列表
const techList = ref([])
//选择检查项目临时列表
const selectTemp = ref({})
const empId = ref()
//模拟医生登录数据
const loginEmp = ref({
    id: '1',
    realname: '扁鹊',
})
//判断是否选择了患者，如果没选择患者不用发送任何请求
if (useRoute().query.caseNumber === undefined) {
    window.alert("请先选择患者")
} else {
    //查询患者信息
    axios({
        method: 'get',
        url: 'register/queryPatientByCaseNum/' + patientInfo.value.caseNumber
    }).then(resp => {
        patientInfo.value = resp.data.data
        if (resp.data.success) {
            queryTech()
        }
    })

}


//根据患者id查询医技项目列表
function queryTech() {
    axios({
        method: 'get',
        url: 'checkRequest/queryCheckById/' + patientInfo.value.id + '/已缴费'
    }).then(resp => {
        techList.value = resp.data.data
        //设置默认选择的项目为第一个
        selectTemp.value = techList.value[0]
    })
}

function handleCurrentChange(val) {
    selectTemp.value = val
}

// 查询所有科室
function queryAllDepartment() {
    axios({
        method: 'get',
        url: 'department/queryAllDepartment'
    }).then(resp => {
        departmentList.value = resp.data.data
    })
}

//选择科室信息
function handleEdit(val) {
    deptmentName.value = departmentList.value[val].deptName
    patientInfo.value.deptmentId = departmentList.value[val].id
    //根据科室信息和排班id查询当前科室的医生
}

//查询排班信息
function querySchedulingId() {
    let weekRule = ''
    if (new Date().getDay() == 1 || new Date().getDay() == 3 || new Date().getDay() == 5) {
        if (patientInfo.value.noon == '上午') {
            weekRule = '00100010001000'
        } else {
            weekRule = '00010001000100'
        }
    }
    if (new Date().getDay() == 2 || new Date().getDay() == 4 || new Date().getDay() == 6) {
        if (patientInfo.value.noon == '上午') {
            weekRule = '00001000100010'
        } else {
            weekRule = '00000100010001'
        }
    }
    if (new Date().getDay() == 6 || new Date().getDay() == 0) {
        weekRule = '11000000000011'
    }
    axios({
        method: 'get',
        url: 'scheduling/querySchedulingByRule/' + weekRule
    }).then(resp => {
        schedulingInfo.value = resp.data.data
    })
}

querySchedulingId()

//查询值班医生列表
function queryDoc(val) {
    patientInfo.value.registMoney = 0
    axios({
        method: 'get',
        url: 'employee/queryDocForCheck/' + patientInfo.value.deptmentId + '/' + schedulingInfo.value.id
    }).then(resp => {
        empList.value = resp.data.data
    })
}

//开始对患者进行检查
function beginCheck() {
//更新checkRequest表中信息,需要医生id和检查申请id
    axios({
        method: 'get',
        url: 'checkRequest/patientBeginCheck/' + empId.value + '/' + selectTemp.value.checkRequestId
    }).then(resp => {
        if (resp.data.success) {
            queryTech()
        }
    })
}
</script>

<style scoped>
.regist-info {
    width: 55%;
}

.regist-info table {
    width: 100%;
    margin: 15px 0;
    border: 0;
}

.regist-info th {
    font-weight: bold;
    background-color: #c6ebfe;
    color: #2073fe
}

.regist-info, .regist-info th, .regist-info td {
    font-size: 0.95em;
    padding: 1.1vw;
    border-collapse: collapse;
}

.regist-info th {
    border: 1px solid #c6ebfe;
    border-width: 1px
}

.regist-info td {
    border: 1px solid #c6ebfe;
    border-width: 1px
}

.regist-info tr {
    border: 1px solid #ffffff;
}

.regist-info tr:nth-child(odd) {
    background-color: #f7f7f7;
}

.regist-info tr:nth-child(even) {
    background-color: #ffffff;
}
</style>